﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.SqlClient;
using System.Text;
using IDAL;
using Models;

namespace DAL
{
    public class ServiceCategoryDAL:IServiceCategory
    {
        public const string SQL_GET_SERVICECATEGORY = @"select * from ServiceCategoryInfo";

        public DataBase DB = new DataBase();

        public List<ServiceCategoryInfo> GetServiceCategory()
        {
            List<ServiceCategoryInfo> list = new List<ServiceCategoryInfo>();
            DB.DatabaseConnection();
            SqlDataReader reader = DB.GetdataRecords(SQL_GET_SERVICECATEGORY);
            while (reader.Read())
            {
                ServiceCategoryInfo servicecategory = new ServiceCategoryInfo();
                ReadtoServiceCategory(reader, servicecategory);
                list.Add(servicecategory);
            }
            DB.CloseDB();
            return list;
        }

        public void ReadtoServiceCategory(SqlDataReader reader,ServiceCategoryInfo servicecategory)
        {
            try
            {
                servicecategory.CategoryID = Int32.Parse(reader["CategoryID"].ToString().Trim());
            }
            catch
            { }
            servicecategory.CategoryName =reader["CategoryName"] is DBNull?"":reader["CategoryName"].ToString();
        }
        public ServiceCategoryInfo GetOneServiceCategoryInfo(int cid)
        {
            ServiceCategoryInfo sc = new ServiceCategoryInfo();
            string sql = "SELECT * FROM ServiceCategoryInfo WHERE CategoryID = " + cid;
            DB.DatabaseConnection();
            SqlDataReader reader = DB.GetdataRecords(sql);
            while (reader.Read())
            {
                ReadtoServiceCategory(reader, sc);
            }
            DB.CloseDB();
            return sc;
        }
    }
}
